#!usr/bin/perl -w

use strict;

die "usage:perl $0 <group1.list> <group2.list> <all_orthomcl.out> <group1 special. out> <group2.special. out> <gene.func>\n" unless @ARGV == 6;

open A,$ARGV[0];    open B,$ARGV[1];	open C,$ARGV[2];	open E,$ARGV[5];
open O1,">$ARGV[3]";
open O2,">$ARGV[4]";
print O1 "ORTHMCLID(genes num,tax num)\tsample:homoloy-gene:function\t...\n";
print O2 "ORTHMCLID(genes num,tax num)\tsample:homoloy-gene:function\t...\n";
my %g1;	my %g2;	my $num1=0;	my $num2=0;	my %func;
while(<E>){
	chomp;my @a=split /\s+/;	$func{$a[0]}=join " ",@a[1..$#a];
}
close E;
while(<A>){
	chomp;my @a=split;	$g1{$a[0]}=1;	$num1++;
}
close A;
while(<B>){
	chomp;my @a=split;	$g2{$a[0]}=1;	$num2++;
}
close B;
while(<C>){
	chomp;my @a=split /\t+/;	my @b=split /[(, ]/,$a[0];	my @c=split /\s+/,$a[-1];
	my $p1=0;	my $p2=0;
	foreach my $k(1..$#c){
		my @d=split /[\(\)]/,$c[$k];
		if(exists $g2{$d[1]}){			$p1++;		}
		if(exists $g1{$d[1]}){                  $p2++;          }
	}
	if($p1 == 0 && $p2 > 0){ 
		print O1 "$a[0]";
		foreach my $k(1..$#c){
			my @d=split /[\(\)]/,$c[$k];
                        print O1 "\t$d[1]:$d[0]:$func{$d[0]}";            
		}
		print O1 "\n";
	}
	if($p2 == 0 && $p1 > 0){ 
        print O2 "$a[0]";                
		foreach my $k(1..$#c){
 		       my @d=split /[\(\)]/,$c[$k];
		       print O2 "\t$d[1]:$d[0]:$func{$d[0]}";
		}
		print O2 "\n";
	}
}
close C;close O1;close O2;
